package com.example.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(CustomEnvironmentPostProcessor.class);

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        logger.info("Processing Environment in CustomEnvironmentPostProcessor");

        try {
            PropertySource<?> propertySource = new ResourcePropertySource(new ClassPathResource("custom-config.properties"));
            environment.getPropertySources().addLast(propertySource);
        } catch (IOException e) {
            logger.error("Failed to load custom-config.properties", e);
        }
    }
}
